package com.example.medicalretrieval;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioGroup;

import com.example.medicalretrieval.adapter.FragAdapter;
import com.example.medicalretrieval.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private ViewPager2 viewPager;
    private RadioGroup rg;
    private List<Fragment> fragmentList;
    private FragAdapter fragAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        viewPager = findViewById(R.id.vp);
        initView();
        initViewPager();
    }

    private void initViewPager() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new SearchFragment());
        fragmentList.add(new HomeFragment());
        fragAdapter = new FragAdapter(getSupportFragmentManager(),getLifecycle(), fragmentList);
        viewPager.setAdapter(fragAdapter);

        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        rg.check(R.id.nav_search);
                        break;
                    case 1:
                        rg.check(R.id.nav_home);
                        break;
                }
            }
        });
    }

    private void initView() {
        viewPager = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);

        rg.setOnCheckedChangeListener(this);
        rg.getChildAt(0).performClick();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.nav_search:
                viewPager.setCurrentItem(0,true);
                break;
            case R.id.nav_home:
                viewPager.setCurrentItem(1,true);
                break;
        }
    }
}